import os
import argparse
import shutil
     
parser = argparse.ArgumentParser(description='读取所有.d文件,并分析所有用到的.c和.h和.S等文件!')
parser.add_argument('--fold', type=str, help='输入文件要分析的文件夹')
args = parser.parse_args()

d_files = set()
c_h_files = set()
for root, dirs, files in os.walk(args.fold):
         for file in files:
             if file.endswith('.d'):
                 d_files.add(os.path.join(root, file))

for file in d_files:
    with open(file, 'r') as f:
        for line in f:
            line = line.rstrip(':\\ \r\n').split()
            for txt in line:
                txt = txt.rstrip(':\\ \r\n').lstrip(':\\\.')
                if not txt.endswith('.o'):
                    c_h_files.add(txt)
for e in c_h_files:
    target_folder = os.path.normpath(os.path.dirname('.\\src\\' + e))
    os.makedirs(target_folder, exist_ok=True)
    shutil.copy2(e, '.\\src\\' + e)